<?php 
RegisterPlugin('aymFreeFour', 'ActivePlugin_aymFreeFour');
function ActivePlugin_aymFreeFour()
{
	global $zbp;	
	Add_Filter_Plugin('Filter_Plugin_Admin_Header', 'aymFreeFour_themeCss');
	Add_Filter_Plugin('Filter_Plugin_Admin_Footer', 'aymFreeFour_themeJs');
	Add_Filter_Plugin('Filter_Plugin_Admin_TopMenu','aymFreeFour_AddMenu');
	Add_Filter_Plugin('Filter_Plugin_Edit_Response5','aymFreeFour_articleMeta');
	Add_Filter_Plugin('Filter_Plugin_Search_Begin','aymFreeFour_SearchPlus_Main');
	Add_Filter_Plugin('Filter_Plugin_Index_Begin','aymFreeFour_modulespublished');
	Add_Filter_Plugin('Filter_Plugin_Search_Begin','aymFreeFour_modulespublished');	
	Add_Filter_Plugin('Filter_Plugin_ViewList_Template','aymFreeFour_modulespublished');	
	Add_Filter_Plugin('Filter_Plugin_ViewPost_Template','aymFreeFour_modulespublished');
	Add_Filter_Plugin('Filter_Plugin_Post_Prev', 'aymFreeFour_Post_Prev');
    Add_Filter_Plugin('Filter_Plugin_Post_Next', 'aymFreeFour_Post_Next');
	if($zbp->Config('aymFreeFour')->seoON == '1'){
		Add_Filter_Plugin('Filter_Plugin_Category_Edit_Response','aymFreeFour_cateSeo');
		Add_Filter_Plugin('Filter_Plugin_Edit_Response','aymFreeFour_articleSeo');
		Add_Filter_Plugin('Filter_Plugin_Tag_Edit_Response','aymFreeFour_tagEdit');
	}
}
function aymFreeFour_AddMenu(&$m){
	global $zbp;
	array_unshift($m, MakeTopMenu("root",'主题配置',$zbp->host . "zb_users/theme/aymFreeFour/settings/main.php","","topmenu_show"));
}
function aymFreeFour_SubMenu($id){
	$aryCSubMenu = array(
		0 => array('基础设置', 'main.php', 'left', false),
		3 => array('SEO设置', 'seo.php', 'left', false),	
		4 => array('广告设置', 'adver.php', 'left', false),
		5 => array('使用帮助', 'http://www.aiyuanma.org/', 'right', false)
	);
	foreach($aryCSubMenu as $k => $v){
		echo '<a href="'.$v[1].'" '.($v[3]==true?'target="_blank"':'').'><span class="m-'.$v[2].' '.($id==$k?'m-now':'').'">'.$v[0].'</span></a>';
	}
}

//初始安装
function InstallPlugin_aymFreeFour(){
	global $zbp;
	if(!$zbp->Config('aymFreeFour')->HasKey('Version')){
		$zbp->Config('aymFreeFour')->Version = '1.0';
		$zbp->Config('aymFreeFour')->favicon='';
		$zbp->Config('aymFreeFour')->avatar='';
		$zbp->Config('aymFreeFour')->about='';
		$zbp->Config('aymFreeFour')->alipay='';
		$zbp->Config('aymFreeFour')->wxpay='';
		$zbp->Config('aymFreeFour')->icp='';
		$zbp->Config('aymFreeFour')->seoON='1';
		$zbp->Config('aymFreeFour')->title='';
		$zbp->Config('aymFreeFour')->keywords='';
		$zbp->Config('aymFreeFour')->description='';
		$zbp->Config('aymFreeFour')->separator='';
		$zbp->Config('aymFreeFour')->copyrightON=0;
		$zbp->Config('aymFreeFour')->boxFirst='';
		$zbp->Config('aymFreeFour')->postStart='';
		$zbp->Config('aymFreeFour')->postBtm='';
		$zbp->SaveConfig('aymFreeFour');
	}
	$zbp->Config('aymFreeFour')->Version = '1.1';
	$zbp->SaveConfig('aymFreeFour');
	aymFreeFour_modules();
}
function UninstallPlugin_aymFreeFour(){
	global $zbp;
	if ($zbp->Config('aymFreeFour')->clearSetting){
		$zbp->DelConfig('aymFreeFour');
	}
	$zbp->db->Delete(
		$zbp->db->sql->Delete(
			$zbp->table['Module'],
			array(
				array('IN','mod_Source',array('custom','theme'))
			)
		)
	);
}
function aymFreeFour_themeCss(){
    global $zbp;
    if (stripos($zbp->currenturl, 'phpinfo') !== false) {
        return;
    }
    echo '<link rel="stylesheet" href="'.$zbp->host.'zb_users/theme/'.$zbp->theme.'/settings/settings.css" type="text/css" media="screen"/>' . "\r\n";    
}
function aymFreeFour_themeJs(){
    global $zbp;
    if (stripos($zbp->currenturl, 'phpinfo') !== false) {
        return;
    }
    if(isset($_GET['act']) && $zbp->CheckPlugin('UEditor')){
    	if($_GET['act']=='ArticleEdt' || $_GET['act']=='PageEdt') {
    		echo '<script src="'.$zbp->host.'zb_users/theme/'.$zbp->theme.'/settings/lib.upload.js"></script>';
    	}elseif($_GET['act']=='CategoryEdt'){
    		echo '<script type="text/javascript" src="'.$zbp->host.'zb_users/settings/UEditor/ueditor.config.php"></script>';
			echo '<script type="text/javascript" src="'.$zbp->host.'zb_users/settings/UEditor/ueditor.all.min.js"></script>';
			echo '<script src="'.$zbp->host.'zb_users/theme/'.$zbp->theme.'/settings/lib.upload.js"></script>';
    	}	
    }
}


function aymFreeFour_articleSeo(){
	global $zbp,$article;
	echo '<div class="boke8net">
			<label>SEO标题</label>
			<input type="text" name="meta_title" class="text" value="'.htmlspecialchars($article->Metas->title).'"/>
			</div>';
	echo '<div class="boke8net">
			<label>SEO关键词</label>
			<input type="text" name="meta_keywords" class="text" placeholder="留空则调用文章标签" value="'.htmlspecialchars($article->Metas->keywords).'"/>
			</div>';
	echo '<div class="boke8net">
			<label>SEO描述</label>
			<textarea name="meta_description" class="text" placeholder="留空则调用文章自动摘要">'.htmlspecialchars($article->Metas->description).'</textarea>
			</div>';
}
function aymFreeFour_cateSeo(){
	global $zbp,$cate;
	echo '<div class="boke8net cateEidtor">
			<label>SEO标题：</label>
			<input name="meta_catetitle" type="text" id="meta_catetitle" class="text" value="'.htmlspecialchars($cate->Metas->catetitle).'"/> </div>';
	echo "\n";		
	echo '<div class="boke8net cateEidtor">
			<label>SEO关键词：</label>
			<input name="meta_catekeywords" type="text" id="meta_catekeywords" class="text" value="'.htmlspecialchars($cate->Metas->catekeywords).'"/> </div>';
	echo "\n";
	echo '<div class="boke8net cateEidtor">
			<label>SEO描述：</label>
			<textarea name="meta_catedescription" id="meta_catedescription" cols="3" rows="6" class="text">'.htmlspecialchars($cate->Metas->catedescription).'</textarea></div>';
}
function aymFreeFour_tagEdit(){
	global $zbp,$tag;
	echo '<div class="boke8net cateEidtor">
			<label>SEO标题：</label>
			<input name="meta_tagtitle" type="text" id="meta_tagtitle" class="text" value="'.htmlspecialchars($tag->Metas->tagtitle).'"/> </div>';
	echo "\n";	
	echo '<div class="boke8net cateEidtor">
			<label>SEO关键词：</label>
			<input name="meta_tagkeywords" type="text" id="meta_tagkeywords" class="text" value="'.htmlspecialchars($tag->Metas->tagkeywords).'"/> </div>';
	echo "\n";
	echo '<div class="boke8net cateEidtor">
			<label>SEO描述：</label>
			<textarea name="meta_tagdescription" id="meta_tagdescription" cols="3" rows="6" class="text">'.htmlspecialchars($tag->Metas->tagdescription).'</textarea></div>';
}
function aymFreeFour_articleMeta(){
	global $zbp,$article;
	if($_GET['act']=='ArticleEdt'){
		echo '<div class="boke8net">
				<label>缩略图</label>
				<div class="uploadimg">
				<input type="text" name="meta_thumburl" class="text uplod_img" value="'.htmlspecialchars($article->Metas->thumburl).'">';
		if ($zbp->CheckPlugin('UEditor')) {	
			echo '<span class="btn">上传图片</span>';
		}
		echo '</div>
			<div class="des">
			</div>';
			if($article->Metas->thumburl){
			echo '<div class="preview">
				<img src="'.$article->Metas->thumburl.'" alt="预览"/>
			</div>';
		}
		echo '</div>';	
	}
}
function aymFreeFour_thumbnail($related) {
    global $zbp;
	if($related->Metas->thumburl){		
		$src = $related->Metas->thumburl;
	}else{
		$pattern="/<[img|IMG].*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/";
		$content = $related->Content; 
		preg_match_all($pattern,$content,$matchContent);
		if(isset($matchContent[1][0])){				
			$src = $thumb=$matchContent[1][0];
		}else{	
			$src = $zbp->host.'zb_users/theme/'.$zbp->theme.'/style/images/no-image.jpg';
		}		
	}
    return $src;
}
function aymFreeFour_intro($as,$type,$long,$other){
	global $zbp;
    $str = '';
    if ($type=='0') {
    $str .= preg_replace('/[\r\n\s]+/', '', trim(SubStrUTF8(TransferHTML($as->Intro,'[nohtml]'),$long)).$other);
    } else {
    $str .= trim(SubStrUTF8(TransferHTML($as->Content,'[nohtml]'),$long)).$other;
    }
    return $str;
}
function aymFreeFour_getArticle($type,$num){
	global $zbp,$str,$order;
	$str = '';
	if($type=='new'){
		$order = array('log_PostTime'=>'DESC');
	}
	if($type=='cmt'){
		$order = array('log_CommNums'=>'DESC');
	}
	if($type=='hot'){
		$order = array('log_ViewNums'=>'DESC');
	}
	$where = array(array('=','log_Status','0'));
	$array = $zbp->GetArticleList(array('*'),$where,$order,array($num),'');
	foreach ($array as $article) {		
		$str .= '<li>';
		$str .= '<a target="_blank" href="'.$article->Url.'" title="'.$article->Title.'">'.$article->Title.'</a>';
		$str .= '</li>';	
		$str .= "\n";	
	}
	return $str;
}
function aymFreeFour_modules(){
	global $zbp;
	
	if(!isset($zbp->modulesbyfilename['cmtpost']))	{
		$t = new Module();
		$t->Name = "热评文章";
		$t->FileName = "cmtpost";
		$t->Source = "theme";
		$t->SidebarID = 0;
		$t->Content = "";
		$t->IsHideTitle=false;
		$t->HtmlID = "cmtpost";
 		$t->Type = "ul";
		$t->MaxLi=5;
		$t->Content = '';
		$t->Save();
	}

	if(!isset($zbp->modulesbyfilename['hotpost']))	{
		$t = new Module();
		$t->Name = "热门文章";
		$t->FileName = "hotpost";
		$t->Source = "theme";
		$t->SidebarID = 0;
		$t->Content = "";
		$t->IsHideTitle=false;
		$t->HtmlID = "hotpost";
 		$t->Type = "ul";
		$t->MaxLi=5;
		$t->Content = '';
		$t->Save();
	}
	if(!isset($zbp->modulesbyfilename['newpost']))	{
		$t = new Module();
		$t->Name = "最新文章";
		$t->FileName = "newpost";
		$t->Source = "theme";
		$t->SidebarID = 0;
		$t->Content = "";
		$t->IsHideTitle=false;
		$t->HtmlID = "newpost";
 		$t->Type = "ul";
		$t->MaxLi=5;
		$t->Content = '';
		$t->Save();
	}
}
function aymFreeFour_cmtpost() {
	global $zbp;
	$i = $zbp->modulesbyfilename['cmtpost']->MaxLi;
	if ($i == 0) $i = 5;
	$s = '';
	$s.= aymFreeFour_getArticle('cmt',$i);
	return $s;
}
function aymFreeFour_hotpost() {
	global $zbp;
	$i = $zbp->modulesbyfilename['hotpost']->MaxLi;
	if ($i == 0) $i = 5;
	$s = '';
	$s.= aymFreeFour_getArticle('hot',$i);
	return $s;
}
function aymFreeFour_newpost() {
	global $zbp;
	$i = $zbp->modulesbyfilename['newpost']->MaxLi;
	if ($i == 0) $i = 5;
	$s = '';
	$s.= aymFreeFour_getArticle('new',$i);
	return $s;
}
function aymFreeFour_modulespublished(){
	global $zbp;
	if(isset($zbp->modulesbyfilename['cmtpost'])){
		$zbp->modulesbyfilename['cmtpost']->Content =aymFreeFour_cmtpost();
	}
	if(isset($zbp->modulesbyfilename['hotpost'])){
		$zbp->modulesbyfilename['hotpost']->Content =aymFreeFour_hotpost();
	}
	if(isset($zbp->modulesbyfilename['newpost'])){
		$zbp->modulesbyfilename['newpost']->Content =aymFreeFour_newpost();
	}
}
function aymFreeFour_Post_Prev(&$getthis) {
    global $zbp;
    $prev=$getthis;
    $articles = $zbp->GetPostList(
        array('*'),
        array(array('=', 'log_Type', 0), array('=', 'log_CateID', $prev->Category->ID),array('=', 'log_Status', 0), array('<', 'log_PostTime', $prev->PostTime)),
        array('log_PostTime' => 'DESC'),
        array(1),
        null
    );
    if (count($articles) == 1) {
        return $articles[0];
    } else {
        return null;
    }
}

function aymFreeFour_Post_Next(&$getthis) {
    global $zbp;
    $prev=$getthis;
    $articles = $zbp->GetPostList(
        array('*'),
        array(array('=', 'log_Type', 0), array('=', 'log_CateID', $prev->Category->ID),array('=', 'log_Status', 0), array('>', 'log_PostTime', $prev->PostTime)),
        array('log_PostTime' => 'ASC'),
        array(1),
        null
    );
    if (count($articles) == 1) {
        return $articles[0];
    } else {
        return null;
    }
}

function aymFreeFour_breadcrumb($id){
	global $zbp, $html;
	$cate = $zbp->categorys;	
	$html =' &gt; <a href="' .$cate[$id]->Url.'" title="查看' .$cate[$id]->Name. '中的全部文章">' .$cate[$id]->Name. '</a> '.$html;
	if(($cate[$id]->ParentID)>0){
		aymFreeFour_breadcrumb($cate[$id]->ParentID);
	}
	return $html;
}

//**代码来自SearchPlus && nobird***/
function aymFreeFour_SubStraymFreeFourUTF8($sourcestr,$aymFreeFourstr,$cutlength) {
	global $zbp;
	if( function_exists('mb_substr') && function_exists('mb_internal_encoding') && function_exists('mb_stripos') ){
		mb_internal_encoding('UTF-8');
		return mb_substr($sourcestr, mb_stripos($sourcestr, $aymFreeFourstr, 0, 'UTF-8'), $cutlength, 'UTF-8');
	}
	if(function_exists('iconv_substr') && function_exists('iconv_strpos') ){		
		ini_set('default_charset', 'UTF-8');
		return iconv_substr($sourcestr, iconv_strpos($sourcestr, $startstr, 0, 'UTF-8'), $cutlength, 'UTF-8');
	}
	$returnstr = '';
	$i = stripos($sourcestr, $aymFreeFourstr);
	$n = 0;
	$str_length = strlen($sourcestr); /*字符串的字节数*/
	while (($n < $cutlength) and ($i <= $str_length)) {
		$temp_str = substr($sourcestr, $i, 1);
		$ascnum = Ord($temp_str); /*得到字符串中第$i位字符的ascii码*/
		if ($ascnum >= 224) /*如果ASCII位高与224，*/
		{

			$returnstr = $returnstr . substr($sourcestr, $i, 3); /*根据UTF-8编码规范，将3个连续的字符计为单个字符*/
			$i = $i + 3; /*实际Byte计为3*/
			$n++; /*字串长度计1*/
		} elseif ($ascnum >= 192) /*如果ASCII位高与192，*/
		{
			$returnstr = $returnstr . substr($sourcestr, $i, 2); /*根据UTF-8编码规范，将2个连续的字符计为单个字符*/
			$i = $i + 2; /*实际Byte计为2*/
			$n++; /*字串长度计1*/
		} elseif ($ascnum >= 65 && $ascnum <= 90) /*如果是大写字母，*/
		{

			$returnstr = $returnstr . substr($sourcestr, $i, 1);
			$i = $i + 1; /*实际的Byte数仍计1个*/
			$n++; /*但考虑整体美观，大写字母计成一个高位字符*/

		} else /*其他情况下，包括小写字母和半角标点符号，*/
		{

			$returnstr = $returnstr . substr($sourcestr, $i, 1);
			$i = $i + 1; /*实际的Byte数计1个*/
			$n = $n + 0.5; /*小写字母和半角标点等与半个高位字符宽...*/

		}
	}
	if ($str_length > $cutlength) {
		$returnstr = $returnstr;
	}

	return $returnstr;

}
function aymFreeFour_SearchPlus_Main() {
    global $zbp;
     
    foreach ($GLOBALS['Filter_Plugin_ViewSearch_Begin'] as $fpname => &$fpsignal) {
        $fpreturn = $fpname();
        if ($fpsignal == PLUGIN_EXITSIGNAL_RETURN) {
            $fpsignal=PLUGIN_EXITSIGNAL_NONE;return $fpreturn;
        }
    }
     
    if(!$zbp->CheckRights($GLOBALS['action'])){Redirect('./');}
 
    $q = trim(htmlspecialchars(GetVars('q','GET')));
    $qc = '<b style=\'color:red\'>' . $q . '</b>';
 
    $articles = array();
    $category = new Metas;
    $author = new Metas;
    $tag = new Metas;
 
	$type = 'post-search';
 
    $zbp->title = $zbp->lang['msg']['search'] . ' &quot;' . $q . '&quot;';
 
    $template = $zbp->option['ZC_INDEX_DEFAULT_TEMPLATE'];
 
    if($zbp->template->hasTemplate('search')){
        $template = 'search';
    }
 
    $w=array();
    $w[]=array('=','log_Type','0');
    if($q){
        $w[]=array('search','log_Content','log_Intro','log_Title',$q);
    }else{
        Redirect('./');
    }
 
    if(!($zbp->CheckRights('ArticleAll')&&$zbp->CheckRights('PageAll'))){
        $w[]=array('=','log_Status',0);
    }
 
$pagebar=new Pagebar('{%host%}search.php?{q='.$q.'}&{page=%page%}',false);
$pagebar->PageCount=$zbp->displaycount; 
$pagebar->PageNow=(int)GetVars('page','GET')==0?1:(int)GetVars('page','GET');
$pagebar->PageBarCount=$zbp->pagebarcount;
 
    $articles = $zbp->GetArticleList(
        '*', 
        $w,
        array('log_PostTime' => 'DESC'), array(($pagebar->PageNow - 1) * $pagebar->PageCount, $pagebar->PageCount),
        array('pagebar' => $pagebar),
        null
    );
    foreach($articles as $article){
        $intro = preg_replace('/[\r\n\s]+/', '', trim(aymFreeFour_SubStraymFreeFourUTF8(TransferHTML($article->Content,'[nohtml]'),$q,170)) . '...');
        $article->Intro = str_ireplace($q,$qc,$intro);
        $article->Title = str_ireplace($q,$qc,$article->Title);
    }
 
    $zbp->header .= '<meta name="robots" content="noindex,follow" />' . "\r\n";
    $zbp->template->SetTags('title', $zbp->title);
    $zbp->template->SetTags('articles',$articles);
    $zbp->template->SetTags('type',$type);
    $zbp->template->SetTags('page',1);
    $zbp->template->SetTags('pagebar',$pagebar);
 
    if ($zbp->template->hasTemplate('search')) {
        $zbp->template->SetTemplate($template);
    } else {
        $zbp->template->SetTemplate('index');
    }
 
    foreach ($GLOBALS['Filter_Plugin_ViewList_Template'] as $fpname => &$fpsignal) {
        $fpreturn=$fpname($zbp->template);
    }
 
    $zbp->template->Display();
    RunTime();
    die();
}
/**代码来自SearchPlus***/
?>